
package com.be.beadmin.quality.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaQueryBuilderNew;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.quality.dto.input.TemplateInspectionItemDto;
import com.be.beadmin.quality.dto.output.TemplateInspectionItemEx;
import com.be.beadmin.quality.entity.TemplateInspectionItem;
import com.be.beadmin.quality.entity.TemplateInspectionItem_;
import com.be.beadmin.quality.mapper.InspectionTemplateMapper;
import com.be.beadmin.quality.mapper.TemplateInspectionItemMapper;
import com.be.beadmin.quality.mapper.TemplateInspectionItemMapperEx;
import com.be.beadmin.quality.repository.InspectionTemplateRepository;
import com.be.beadmin.quality.repository.TemplateInspectionItemRepository;
import com.be.beadmin.quality.service.TemplateInspectionItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author ljh
* @date 2023-05-18
**/
@Service
@RequiredArgsConstructor
public class TemplateInspectionItemServiceImpl implements TemplateInspectionItemService {

    @Autowired
    private  TemplateInspectionItemRepository templateInspectionItemRepository;
    @Autowired
    private  TemplateInspectionItemMapper templateInspectionItemMapper;
    @Autowired
    private  TemplateInspectionItemMapperEx templateInspectionItemExMapper;

    @Autowired
    private InspectionTemplateRepository inspectionTemplateRepository;
    @Autowired
    private InspectionTemplateMapper inspectionTemplateMapper;

    @Override
    public Page<TemplateInspectionItemEx> getTemplateInspectionItemByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<TemplateInspectionItem> page = templateInspectionItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilderNew.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(TemplateInspectionItem_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(TemplateInspectionItem_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<TemplateInspectionItemEx> page1 = page.map(templateInspectionItemExMapper::toEntity);
            return page1;

    }

    @Override
    public void addTemplateInspectionItem(TemplateInspectionItemDto templateInspectionItemDto) {
            TemplateInspectionItem result = templateInspectionItemRepository.findTemplateInspectionItemByIdAndDelete(templateInspectionItemDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("TemplateInspectionItem不存在,请重新查询");
            }
            TemplateInspectionItem templateInspectionItem = templateInspectionItemMapper.toEntity(templateInspectionItemDto);
            templateInspectionItemRepository.save(templateInspectionItem);
    }

    @Override
    public void updateTemplateInspectionItem(TemplateInspectionItemDto templateInspectionItemDto) {
            TemplateInspectionItem templateInspectionItem = templateInspectionItemRepository.findTemplateInspectionItemById(templateInspectionItemDto.getId());
            templateInspectionItem = templateInspectionItemMapper.toEntity(templateInspectionItemDto);
            templateInspectionItemRepository.save(templateInspectionItem);
    }


    @Override
    public void delTemplateInspectionItem(String id) {
        TemplateInspectionItem templateInspectionItem = templateInspectionItemRepository.findTemplateInspectionItemById(id);
        templateInspectionItem.setDelete(1);
        templateInspectionItemRepository.save(templateInspectionItem);
    }

    @Override
    public void delTemplateInspectionItems (List<String> ids) { }

    @Override
    public List<TemplateInspectionItemDto> getTemplateInspectionItem(String inspectionTemplateId) {
        List<TemplateInspectionItem> templateInspectionItemList = templateInspectionItemRepository.findTemplateInspectionItemByInspectionTemplateIdAndDelete(inspectionTemplateId,0);
        return templateInspectionItemMapper.toDto(templateInspectionItemList);
    }

    @Override
    public TemplateInspectionItem getTemplateInspectionItemBySortId(String inspectionTemplateId, int sortId) {
        if(inspectionTemplateId==null) throw new BeadminException("未指定检验表模板id");
        if(sortId<=0) throw new BeadminException("未指定排序序号或序号不是正整数");
        TemplateInspectionItem templateInspectionItem=templateInspectionItemRepository.findTemplateInspectionItemByInspectionTemplateIdAndSortIdAndDelete(inspectionTemplateId,sortId,0);
        return templateInspectionItem;
    }
}